﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ras2Logic;

namespace Ras2Win
{
    public partial class SongView : UserControl
    {
        #region Member breytur

        #endregion

        #region Smiðir

        /// <summary>
        /// Færibreytulaus smiður
        /// </summary>
        public SongView()
        {
            InitializeComponent();
        }

        #endregion

        #region Get / Set föll

        /// <summary>
        /// GetSet fall fyrir breytuna SelectedSongID sem er af taginu int.
        /// </summary>
        public int SelectedSongID { get; set; }

        #endregion

        #region Private föll

        /// <summary>
        /// Fall sem keyrist þegar SongView er loadað.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SongView_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                SongController ctrl = new SongController();
                foreach (var item in ctrl.GetAllSongs())
                {
                    m_lstViewSongs.Items.Add(CreateListViewItem(item));
                }
            }
        }

        /// <summary>
        /// Hjálparfall sem smíðar listview item
        /// </summary>
        /// <param name="song">Tekur inn objectið Song</param>
        /// <returns>Skilar ListViewItemi</returns>
        private ListViewItem CreateListViewItem(Song song)
        {
            ListViewItem item = new ListViewItem(Convert.ToString(song.ID));
            item.SubItems.Add(song.Name);

            item.ImageIndex = 0;
            item.Tag = song;
            return item;
        }

        /// <summary>
        /// Fall sem keyrist þegar vali er breytt í listanum
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_lstViewSongs_SelectedIndexChanged(object sender, EventArgs e)
        {
            Song activeSong = new Song();
            try
            {
                activeSong.ID = Convert.ToInt32(m_lstViewSongs.SelectedItems[0].Text);
            }
            catch (Exception)
            {
                //Gerum bara ekkert.
            }
            SelectedSongID = activeSong.ID;
        }

        #endregion

        #region Public föll

        /// <summary>
        /// Fall uppfærir lag
        /// </summary>
        public void OnEditClick()
        {
            if (m_lstViewSongs.SelectedItems.Count == 1)
            {
                ListViewItem listViewItem = m_lstViewSongs.SelectedItems[0];

                Song song = (Song)listViewItem.Tag;
                using (EditSong dlg = new EditSong())
                {
                    dlg.Song = song;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        //Sækjum gögnin aftur úr dialog
                        song = dlg.Song;

                        SongController ctrl = new SongController();

                        //TODO: Setja try catch
                        ctrl.UpdateSong(song);

                        //Grípa indexinn.
                        int nIndex = listViewItem.Index;
                        //Við uppfærum færsluna með því að fjarlægja þá sem
                        //er fyrir og setja nýja inn
                        m_lstViewSongs.Items.Remove(listViewItem);
                        m_lstViewSongs.Items.Insert(nIndex, CreateListViewItem(song));
                    }
                }
            }
        }

        /// <summary>
        /// Fall sem setur inn nýtt lag.
        /// </summary>
        public void OnNewClick()
        {
            using (EditSong dlg = new EditSong())
            {
                dlg.Song = new Song();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Song song = dlg.Song;
                    SongController controller = new SongController();

                    //TODO: Setja try catch blokk
                    controller.AddSong(song);
                    m_lstViewSongs.Items.Add(CreateListViewItem(song));
                }
            }
        }

        #endregion

    }
}
